import json

import scrapy


class BaizimanhuaSpider(scrapy.Spider):
    name = "baizimanhua"
    async def start(self):
        headers = {
            'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
            'referer' : 'https://cn.baozimhcn.com/user/signin',
        }
        yield scrapy.FormRequest("https://cn.baozimhcn.com/api/bui/signin",callback=self.parse,formdata=({"username":"794142023@qq.com","password":"Whbhh20010708."}),method="POST",headers=headers)


    async def parse(self, response):
        data = json.loads(response.text)
        token = data.get('data')
        if token:
            cookies = {
                'TSID': token  # 服务器会用这个token设置TSID cookie
            }
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
                'referer': 'https://cn.baozimhcn.com/user/signin',
            }
            yield scrapy.FormRequest('https://cn.baozimhcn.com/user/my_bookshelf',callback=self.parse2,headers=headers,cookies=cookies)

    async def parse2(self, response):
        data = response.xpath('//div[@class="bookshelf-items"]/div[@class="info"]/ul/li/h4/a/text()').getall()
        print(data)


